//
//  ShoppingViewController.m
//  何五路
//
//  Created by _ADY on 15/6/5.
//  Copyright (c) 2015年 _ADY. All rights reserved.
//

#import "ShoppingViewController.h"
#import "GoodsDetailsViewController.h"
#import "SettlementViewController.h"
@interface ShoppingViewController ()

@end
static LoadingView *loading = nil;

#define tabeHight 110
#define reTag 100
#define reDTag 1231000
#define addTag 1310000
#define textTag 1210000
#define gTag 1110000
#define detailTag 14210000
#define detailsTag 101000
@implementation ShoppingViewController
@synthesize mTableView,shopArray,shopByArray,allArray,aBarString;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        self.title = @"购物车";
        self.view.backgroundColor = bgColor;
        shopArray = [[NSMutableArray alloc] init];
        shopByArray = [[NSMutableArray alloc] init];
        allArray = [[NSMutableArray alloc] init];
        
        gArray = [[NSMutableArray alloc] init];
        xhArray = [[NSMutableArray alloc] init];
    }
    return self;
}

-(void)rightAction
{
    addButton.hidden = YES;
    addButton1.hidden = NO;
    addButton2.hidden = NO;
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"完成" style:UIBarButtonItemStylePlain target:self action:@selector(right1Action)];
}

-(void)right1Action
{
    addButton.hidden = NO;
    addButton1.hidden = YES;
    addButton2.hidden = YES;
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"编辑" style:UIBarButtonItemStylePlain target:self action:@selector(rightAction)];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithTitle:@"" style:UIBarButtonItemStylePlain target:nil action:nil];
    self.navigationItem.backBarButtonItem = item;
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"编辑" style:UIBarButtonItemStylePlain target:self action:@selector(rightAction)];
    
    
    mTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, screenMySize.size.width, screenMySize.size.height-48) style:UITableViewStyleGrouped];
    mTableView.delegate = self;
    mTableView.dataSource = self;
    mTableView.sectionHeaderHeight = 0.0;
    mTableView.sectionFooterHeight = 0.0;
    mTableView.backgroundColor = [UIColor clearColor];
    [self.view addSubview:mTableView];
    
    __weak __typeof(self) weakSelf = self;
    
    mTableView.header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [weakSelf loadNewData];
    }];
    
//    [mTableView.header beginRefreshing];

    [self myBarView];
}

-(void)myBarView
{
    barView = [[UIView alloc] init];
    if ([aBarString isEqualToString:@"1"])
    {
        barView.frame = CGRectMake(0, screenMySize.size.height-60, screenMySize.size.width, 60);
    }else
        barView.frame = CGRectMake(0, screenMySize.size.height-48-60, screenMySize.size.width, 60);
    
    barView.backgroundColor = [UIColor colorWithWhite:0 alpha:.8];
    [self.view addSubview:barView];
    
    yesBool = NO;
    UIImage *imageD = [UIImage imageNamed:@"xzPress"];
    allButton = [UIButton buttonWithType:UIButtonTypeCustom];
    CGRect frame = CGRectMake(0,(barView.frame.size.height-30)/2, 70, 30);
    [allButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    allButton.titleLabel.font = [UIFont fontWithName:@"Arial" size:labelSize-1];
    [allButton setTitle:@"全选" forState:UIControlStateNormal];
    [allButton setFrame:frame];
    [allButton setTitleEdgeInsets:UIEdgeInsetsMake(0, imageD.size.width, 0, 0)];
    [allButton setImageEdgeInsets:UIEdgeInsetsMake(0, 10, 0, 0)];
    [allButton setImage:imageD forState:UIControlStateNormal];
    [allButton addTarget:self action:@selector(allAction) forControlEvents:UIControlEventTouchUpInside];
    [barView addSubview:allButton];
    
    
    combinedLabel = [[UILabel alloc] initWithFrame:CGRectMake(80,0, screenMySize.size.width*2/3-80, barView.frame.size.height*2/3-5)];
    combinedLabel.text = [NSString stringWithFormat:@"合计¥0.00"];
    combinedLabel.font = [UIFont fontWithName:@"Arial" size:labelSize+3];
    combinedLabel.textColor = [UIColor whiteColor];
    [barView addSubview:combinedLabel];
    
    totalLabel = [[UILabel alloc] initWithFrame:CGRectMake(80,barView.frame.size.height*2/3-5, screenMySize.size.width*2/3-80, barView.frame.size.height/3)];
    totalLabel.text = [NSString stringWithFormat:@"总额¥0.00"];
    totalLabel.font = [UIFont fontWithName:@"Arial" size:labelSize-2];
    totalLabel.textColor = [UIColor whiteColor];
    [barView addSubview:totalLabel];
    
    addButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [addButton setFrame:CGRectMake(screenMySize.size.width*2/3, 0, screenMySize.size.width*1/3, barView.frame.size.height)];
    [addButton setBackgroundColor:[UIColor colorWithRed:238/255.0 green:84/255.0 blue:84/255.0 alpha:1]];
    [addButton setTitle:@"去结算" forState:UIControlStateNormal];
    [addButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [addButton addTarget:self action:@selector(settlementAction) forControlEvents:UIControlEventTouchUpInside];
    [barView addSubview:addButton];
    addButton.hidden = NO;
    
    addButton1 = [UIButton buttonWithType:UIButtonTypeCustom];
    [addButton1 setFrame:CGRectMake(80, 0, (screenMySize.size.width-80)/2, barView.frame.size.height)];
    [addButton1 setBackgroundColor:[UIColor grayColor]];
    [addButton1 setTitle:@"移动收藏夹" forState:UIControlStateNormal];
    [addButton1 setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [addButton1 addTarget:self action:@selector(addSCAction) forControlEvents:UIControlEventTouchUpInside];
    [barView addSubview:addButton1];
    addButton1.hidden = YES;
    
    addButton2 = [UIButton buttonWithType:UIButtonTypeCustom];
    [addButton2 setFrame:CGRectMake(80+(screenMySize.size.width-80)/2, 0, (screenMySize.size.width-80)/2, barView.frame.size.height)];
    [addButton2 setBackgroundColor:[UIColor colorWithRed:238/255.0 green:84/255.0 blue:84/255.0 alpha:1]];
    [addButton2 setTitle:@"删除" forState:UIControlStateNormal];
    [addButton2 setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [addButton2 addTarget:self action:@selector(deleAction) forControlEvents:UIControlEventTouchUpInside];
    [barView addSubview:addButton2];
    addButton2.hidden = YES;
    
}

-(NSMutableDictionary*)removefromcart
{
    NSMutableDictionary *postDic1 = [[NSMutableDictionary alloc] init];
    
    for (int j = 0; j < gArray.count; j++)
    {
        NSMutableArray *m = [gArray objectAtIndex:j];
        NSMutableArray *m1 = [shopArray objectAtIndex:j];
        for ( int i = 0; i < m.count; i ++)
        {
            for (int k = 0; k < [[m objectAtIndex:i] count]; k++)
            {
                NSMutableDictionary *FlowSetting = [NSMutableDictionary dictionaryWithDictionary:[[m objectAtIndex:i] objectAtIndex:k]];
                if ([[NSString stringWithFormat:@"%@",[FlowSetting objectForKey:@"gKey"]] isEqualToString:@"1"] )
                {
                    [postDic1 setObject:[[[m1 objectAtIndex:i] objectAtIndex:k] objectForKey:@"Id"] forKey:[NSString stringWithFormat:@"removefromcart%d",i*100+k+100000*j]];
                }
            }
        }
    }
    
    return postDic1;
}

-(void)goloading
{
    loading = [[LoadingView alloc] initWithFrame:self.view.frame];
    [self.view addSubview:loading];
}
#pragma mark 移动收藏夹
-(void)addSCAction
{
    if (![[NSUserDefaults standardUserDefaults] boolForKey:@"isLoginKey"]) {
        [self loginAction];
        return;
    }
    
    [self goloading];
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        NSDictionary *myArray = [SendService AddItemsToWishlistFromCart:[self removefromcart]];
        dispatch_async(dispatch_get_main_queue(), ^{
             [self setDic:myArray];
            [self setGWCcount];
            [loading closeLoading];
        });
    });

}
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [mTableView.header beginRefreshing];
    if ([aBarString isEqualToString:@"1"])
    {
        barView.frame = CGRectMake(0, screenMySize.size.height-60, screenMySize.size.width, 60);
    }else
        barView.frame = CGRectMake(0, screenMySize.size.height-48-60, screenMySize.size.width, 60);
    
    [self setGWCcount];
    
}

-(void)setGWCcount
{
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0), ^{
        NSDictionary *ret = [SendService GetShoppingCartCount];
        dispatch_async(dispatch_get_main_queue(), ^{
            
            if (ret !=nil)
            {
                if ([[ret objectForKey:@"Result"] isEqualToString:@"success"])
                {
                    NSString *aStr = [NSString stringWithFormat:@"%@",[[ret objectForKey:@"Entity"] objectForKey:@"ShoppingCartCount"]];
                    if ([aStr isEqualToString:@"0"])
                    {
                        return ;
                    }
                    [[NSUserDefaults standardUserDefaults] setObject:aStr forKey:@"ShoppingCartCountKey"];
                    NSArray* viewConrolers = [self.tabBarController viewControllers];
                    ShoppingViewController* vc = [viewConrolers objectAtIndex:2];
                    vc.tabBarItem.badgeValue = aStr;
                }
            }
            
        });
    });
}

-(NSMutableDictionary*)hidOperate:(BOOL)hidBool
{
    NSMutableDictionary *postDic1 = [[NSMutableDictionary alloc] init];
    
    if (hidBool)
        [postDic1 setObject:@"DEL" forKey:@"hidOperate"];//删除
    
    for (int j = 0; j < gArray.count; j++)
    {
        NSMutableArray *m = [gArray objectAtIndex:j];
        NSMutableArray *m1 = [shopArray objectAtIndex:j];
        for ( int i = 0; i < m.count; i ++)
        {
            for (int k = 0; k < [[m objectAtIndex:i] count]; k++)
            {
                
                NSMutableDictionary *FlowSetting = [NSMutableDictionary dictionaryWithDictionary:[[m objectAtIndex:i] objectAtIndex:k]];
                if ([[NSString stringWithFormat:@"%@",[FlowSetting objectForKey:@"gKey"]] isEqualToString:@"1"] && !hidBool)
                {
                    [postDic1 setObject:[[[m1 objectAtIndex:i] objectAtIndex:k] objectForKey:@"Id"] forKey:[NSString stringWithFormat:@"useAct%d",i*100+k+100000*j]];
                }
                
                if (!hidBool||(hidBool &&[[NSString stringWithFormat:@"%@",[FlowSetting objectForKey:@"gKey"]] isEqualToString:@"1"]))
                {
                    [postDic1 setObject:[[[m1 objectAtIndex:i] objectAtIndex:k] objectForKey:@"Id"] forKey:[NSString stringWithFormat:@"removefromcart%d",i*100+k+100000*j]];
                    [postDic1 setObject:[[[m1 objectAtIndex:i] objectAtIndex:k] objectForKey:@"Quantity"] forKey:[NSString stringWithFormat:@"itemquantity%@",[[[m1 objectAtIndex:i] objectAtIndex:k] objectForKey:@"Id"]]];
                }

          
            }
        }
    }
    
    return postDic1;
}

#pragma mark 更新
-(void)updata
{
    [self goloading];
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        NSDictionary *ret2 = [SendService UpdateCart:[self hidOperate:NO]];;
        dispatch_async(dispatch_get_main_queue(), ^{
            [loading closeLoading];
            isUpdata = YES;
            [self setDic:ret2];
            
        });
    });
}
#pragma mark 删除
-(void)deleAction
{
    if (![[NSUserDefaults standardUserDefaults] boolForKey:@"isLoginKey"]) {
        [self loginAction];
        return;
    }
    [self goloading];
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        [SendService UpdateCart:[self hidOperate:YES]];
        dispatch_async(dispatch_get_main_queue(), ^{
            
            [self right1Action];
            [mTableView.header beginRefreshing];
            
            [self setGWCcount];
            [loading closeLoading];
        });
    });
}

-(void)loginAction
{
    LoginViewController *browser = [[LoginViewController alloc] init];
    browser.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:browser animated:YES];
}

#pragma mark 结算
-(void)settlementAction
{
    if (![[NSUserDefaults standardUserDefaults] boolForKey:@"isLoginKey"]) {
        [self loginAction];
        return;
    }
    
    [self goloading];

    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
                NSDictionary *myArray = [SendService CheckoutOrder:[self removefromcart]];
                dispatch_async(dispatch_get_main_queue(), ^{
                    if (![[NSString stringWithFormat:@"%@",[myArray objectForKey:@"Result"]] isEqualToString:@"error"]) {
                        
                        SettlementViewController *details = [[SettlementViewController alloc] init];
                        details.allArray = myArray;
                        details.hidesBottomBarWhenPushed = YES;
                        [self.navigationController pushViewController:details animated:YES];
                    }
                    else
                    {
                        [SendService alerViewMessage:[myArray objectForKey:@"Message"]];
                    }
                    [loading closeLoading];
                    NSLog(@"结算 %@",myArray);
                });
            });

}

#pragma mark 全选
-(void)allAction
{
    [self allActionButton];
    for (int j = 0; j < gArray.count; j++)
    {
        NSMutableArray *m = [gArray objectAtIndex:j];
        for ( int i = 0; i < m.count; i ++)
        {
            for (int k = 0; k < [[m objectAtIndex:i] count]; k++)
            {
                
                NSMutableDictionary *FlowSetting = [NSMutableDictionary dictionaryWithDictionary:[[m objectAtIndex:i] objectAtIndex:k]];
                if (yesBool)
                {
                    if (![[NSString stringWithFormat:@"%@",[[[[shopArray objectAtIndex:j] objectAtIndex:i] objectAtIndex:k] objectForKey:@"Disabled"]] isEqualToString:@"0"])
                    {
                        [FlowSetting setValue:@"0" forKey:@"gKey"];
                        [FlowSetting setValue:@"1" forKey:@"allgKey"];

                    }else
                    {
                        [FlowSetting setValue:@"1" forKey:@"gKey"];
                        [FlowSetting setValue:@"1" forKey:@"allgKey"];
                    }
                }
                else
                {
                    [FlowSetting setValue:@"0" forKey:@"gKey"];
                    [FlowSetting setValue:@"0" forKey:@"allgKey"];
                }
                [[[gArray objectAtIndex:j] objectAtIndex:i] setObject:FlowSetting atIndexedSubscript:k];
            }
        }
    }
    [self updata];
//    [mTableView reloadData];
//    [self allMoney];
}

-(void)allActionButton
{
    yesBool = !yesBool;
    if (yesBool)
    {
        [allButton setImage:[UIImage imageNamed:@"xzPress"] forState:UIControlStateNormal];
    }else
        [allButton setImage:[UIImage imageNamed:@"xzPre"] forState:UIControlStateNormal];
}

#pragma mark 合计多少钱
-(void)allMoney
{
    float allMoney = 0.00;
    bool iBool = NO;
    
    for (int i = 0; i < shopByArray.count; i++)
    {
        NSDictionary *shopD = [shopByArray objectAtIndex:i];
        for (int j = 0; j < [[shopArray objectAtIndex:i] count]; j++)
        {
            NSMutableArray *dic = [[shopArray objectAtIndex:i] objectAtIndex:j];
            
            for (int k = 0; k < dic.count; k ++)
            {
                if (![[NSString stringWithFormat:@"%@",[[dic objectAtIndex:k] objectForKey:@"ActProductInfo"]] isEqualToString:@"<null>"])
                {
                    NSString *PicStr = nil;
                    
                    for (NSDictionary *shopM in [shopD objectForKey:@"VendorActProductInfoList"])
                    {
                        if ([[NSString stringWithFormat:@"%@",[[[dic objectAtIndex:0] objectForKey:@"ActProductInfo"]objectForKey:@"ActId" ]] isEqualToString:[NSString stringWithFormat:@"%@",[[shopM objectForKey:@"ActProductInfo"]objectForKey:@"ActId" ]]])
                        {
                            PicStr = [NSString stringWithFormat:@"%.2f",[[shopM objectForKey:@"ActAmount"] floatValue]-[[shopM objectForKey:@"ActDeductionAmount"]floatValue]];
                            allMoney = allMoney+[PicStr floatValue];
                            break;
                        }
                    }
                }
                else
                {

                    if ([[NSString stringWithFormat:@"%@",[[[[gArray objectAtIndex:i] objectAtIndex:j] objectAtIndex:k] objectForKey:@"gKey"]] isEqualToString:@"1"])
                    {
                        NSString *price = [NSString stringWithFormat:@"%@",[[[[shopArray objectAtIndex:i] objectAtIndex:j] objectAtIndex:k] objectForKey:@"CustomerDiscountUnitPrice"]];
                        price = [self htmlEntityDeco:price];
                        
                        
                        NSString *price1 = [NSString stringWithFormat:@"%@",[[[[shopArray objectAtIndex:i] objectAtIndex:j] objectAtIndex:k] objectForKey:@"Quantity"]];
                        
                        allMoney = allMoney+[price1 floatValue]*[price floatValue];
                    }
                    
                }
                
                if([[NSString stringWithFormat:@"%@",[[[[gArray objectAtIndex:i] objectAtIndex:j] objectAtIndex:k] objectForKey:@"allgKey"]] isEqualToString:@"0"])
                {
                    iBool = YES;
                }
            }
        }
    }
    
    if (iBool)
    {
        yesBool = YES;
    }
    else
        yesBool = NO;
    [self allActionButton];

    totalLabel.text = [NSString stringWithFormat:@"总额¥%.2f",allMoney];
    combinedLabel.text = [NSString stringWithFormat:@"合计¥%.2f",allMoney];
}

-(NSString *)htmlEntityDeco:(NSString *)string
{
    string = [string stringByReplacingOccurrencesOfString:@"$" withString:@""];
    string = [string stringByReplacingOccurrencesOfString:@"¥" withString:@""];
    
    return string;
}

-(void)setDic:(NSDictionary*)ret2
{
    NSLog(@"ret1 %@",ret2);
    if ([[NSString stringWithFormat:@"%@",[ret2 objectForKey:@"Result"]] isEqualToString:@"error"] ||ret2 ==nil)
    {
        [mTableView.header endRefreshing];
        return;
    }
    
    
    allArray = [ret2 objectForKey:@"Entity"];
    shopByArray = [[ret2 objectForKey:@"Entity"] objectForKey:@"VendorItems"];
    NSMutableArray *meArray = [[NSMutableArray alloc] init];
    meArray = [[ret2 objectForKey:@"Entity"] objectForKey:@"Items"];
    
    if (!isUpdata)
    {
        if (gArray != nil) {
            [gArray removeLastObject];
            gArray = nil;
        }
        gArray = [[NSMutableArray alloc] init];
    }
    if (shopArray != nil) {
        [shopArray removeLastObject];
        shopArray = nil;
    }
    shopArray = [[NSMutableArray alloc] init];
  
    /*
     请求地址：/ApiShoppingCart/Cart
     请求方式：Post
     请求参数：null
     返回数据：|
     foreach (var vendorItem in Model.VendorItems)//循环每个店铺
     
     foreach(var vendorActProductInfo  in vendorItem.VendorActProductInfoList)//循环店铺活动列表
     
     
     vendorActProductInfo.ActProductInfo//活动详细信息
     
     vendorActProductInfo.ActDeductionAmount//活动抵扣金额
     
     vendorActProductInfo.ActAmount//活动金额
     
     vendorActProductInfo.ActShowTitle//活动标题
     
     vendorActProductInfo.ActShowInfo//活动优惠信息
     
     vendorActProductInfo.ActLink//活动链接
     
     vendorActProductInfo.ActShoppingCartItemsModel//活动购物车商品列表
     */
    for (NSDictionary *dic in shopByArray)
    {
        NSString *vId = [NSString stringWithFormat:@"%@",[dic objectForKey:@"VendorId"]];
        
        NSMutableArray *de = [[NSMutableArray alloc] init];
        
        NSMutableArray *hdArray = [[NSMutableArray alloc] init];
        NSMutableArray *notHdArray = [[NSMutableArray alloc] init];
        

        
        for (NSDictionary *d in meArray)
        {
      
            NSMutableDictionary *de1 = [[NSMutableDictionary alloc] init];
            if (![[NSString stringWithFormat:@"%@",[d objectForKey:@"Disabled"]] isEqualToString:@"0"])//下架
            {
                [de1 setValue:@"0" forKey:@"gKey"];
                [de1 setValue:@"1" forKey:@"allgKey"];
            }
            else
            {
                [de1 setValue:@"1" forKey:@"gKey"];
                [de1 setValue:@"1" forKey:@"allgKey"];
            }
            
            if ([[NSString stringWithFormat:@"%@",[d objectForKey:@"VendorId"]] isEqualToString:vId])//同一店铺商品
            {
                if (![[NSString stringWithFormat:@"%@",[d objectForKey:@"ActProductInfo"]] isEqualToString:@"<null>"])//活动商品
                {
                    BOOL have = NO;

                    for (int i = 0; i < hdArray.count; i++)
                    {
                        NSMutableArray *di = [hdArray objectAtIndex:i];
                        if (![[NSString stringWithFormat:@"%@",[[di objectAtIndex:0] objectForKey:@"ActProductInfo"]] isEqualToString:@"<null>"])
                        {
                            if ([[NSString stringWithFormat:@"%@",[[[di objectAtIndex:0] objectForKey:@"ActProductInfo"]objectForKey:@"ActId"]] isEqualToString:[NSString stringWithFormat:@"%@",[[d objectForKey:@"ActProductInfo"]objectForKey:@"ActId"]]])//相同活动放一起
                            {
                                [[de objectAtIndex:i] addObject:de1];
                                [[hdArray objectAtIndex:i] addObject:d];
                                have = YES;
                                continue;
                            }
                        }
  
                    }
                    if (!have)
                    {
                        NSMutableArray *hArray = [[NSMutableArray alloc] init];
                        [hArray addObject:d];
                        [hdArray addObject:hArray];
                        
                        NSMutableArray *dArray = [[NSMutableArray alloc] init];
                        [dArray addObject:de1];
                        
                         [de addObject:dArray];
                    }
                   
                }
                else//没活动商品
                {
                    NSMutableArray *hArray = [[NSMutableArray alloc] init];
                    [hArray addObject:d];
                    [notHdArray addObject:hArray];
                    
                    NSMutableArray *dArray = [[NSMutableArray alloc] init];
                    [dArray addObject:de1];
                    
                    [de addObject:dArray];
                }
            }
        }
        [hdArray addObjectsFromArray:notHdArray];
        [shopArray addObject:hdArray];
        if (!isUpdata)
        {
            [gArray addObject:de];
        }
    }
    if (isUpdata)
        isUpdata = NO;
    [mTableView.header endRefreshing];
    [mTableView reloadData];
    [self allMoney];
}
- (void)loadNewData
{
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        NSDictionary *ret2 = [SendService cart];
        dispatch_async(dispatch_get_main_queue(), ^{

            isUpdata = NO;
            [self setDic:ret2];

        });
    });
    
    
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        NSDictionary *myArray = [SendService InterestingProducts];
        dispatch_async(dispatch_get_main_queue(), ^{
//            NSLog(@"猜你喜欢 %@",myArray);
             if (![[NSString stringWithFormat:@"%@",[myArray objectForKey:@"Result"]] isEqualToString:@"error"])
            {
                xhArray = [myArray objectForKey:@"Entity"];

                [mTableView reloadData];
            }
            
        });
    });
    
}

#pragma mark -
#pragma mark Table View DataSource

- (CGFloat) tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (section == 0)
        return 0.001;
    return 10;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    
    return shopByArray.count+1;
}

- (NSInteger)tableView:(UITableView *)aTableView numberOfRowsInSection:(NSInteger)section
{
    if (section == shopByArray.count)
        return 2;
    NSMutableArray *d = [shopArray objectAtIndex:section];
    return d.count+1;

}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{

   if (indexPath.section == shopByArray.count)
   {
       if (indexPath.row == 0) {
           return 40;
       }
       return tabeHight+70;
   }
    else
    {
        if (indexPath.row == 0) {
            return 40;
        }
        NSMutableArray *d = [[shopArray objectAtIndex:indexPath.section] objectAtIndex:indexPath.row-1];

        if (![[NSString stringWithFormat:@"%@",[[d objectAtIndex:0] objectForKey:@"ActProductInfo"]] isEqualToString:@"<null>"])
                return tabeHight*d.count+60;

        return tabeHight*d.count;
    }

    return YES;
}

- (UITableViewCell *)tableView:(UITableView *)aTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [aTableView dequeueReusableCellWithIdentifier:CellIdentifier];
    
    if (cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        aTableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
        aTableView.showsVerticalScrollIndicator = NO;
    }
    int i = (int)[[cell.contentView subviews] count] - 1;
    for(;i >= 0 ; i--)
    {
        [[[cell.contentView subviews] objectAtIndex:i] removeFromSuperview];
    }
    cell.accessoryType = UITableViewCellAccessoryNone;
    cell.backgroundColor = [UIColor whiteColor];
    
    if (indexPath.section == shopByArray.count)
        return [self mCell:cell bCTableView:aTableView cellForRowAtIndexPath:indexPath];
    else
        return [self mCell:cell aCTableView:aTableView cellForRowAtIndexPath:indexPath];

    return cell;
}

- (UITableViewCell *)mCell:(UITableViewCell*)cell aCTableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    if (indexPath.row == 0)
    {
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        [cell.contentView addSubview:[self setButton:CGRectMake(10, 9, 22, 22) setBool:NO setTag:(indexPath.section+2)*gTag]];
        
        [cell.contentView addSubview:[self setFrame:CGRectMake(35, 10, screenMySize.size.width-45, 20) setTitle:[NSString stringWithFormat:@"%@",[[shopByArray objectAtIndex:indexPath.section]  objectForKey:@"VendorName"]] setFont:labelSize setColor:[UIColor blackColor]]];
    }
    else
    {
        int Hight = 0;

        NSMutableArray *dic = [[shopArray objectAtIndex:indexPath.section] objectAtIndex:indexPath.row-1];

        for (int i = 0; i < dic.count; i ++)
        {
            if (![[NSString stringWithFormat:@"%@",[[dic objectAtIndex:0] objectForKey:@"ActProductInfo"]] isEqualToString:@"<null>"])
                Hight = 30;
            
            if (Hight == 30)//有活动，显示活动信息
            {
                NSString *MStr = nil;
                NSString *PicStr = nil;
                NSString *Pic1Str = nil;
                NSDictionary *shopD = [shopByArray objectAtIndex:indexPath.section];
                for (NSDictionary *shopM in [shopD objectForKey:@"VendorActProductInfoList"]) {
                    if ([[NSString stringWithFormat:@"%@",[[[dic objectAtIndex:0] objectForKey:@"ActProductInfo"]objectForKey:@"ActId" ]] isEqualToString:[NSString stringWithFormat:@"%@",[[shopM objectForKey:@"ActProductInfo"]objectForKey:@"ActId"]]])
                    {
                        MStr = [NSString stringWithFormat:@"%@",[shopM objectForKey:@"ActShowInfo"]];
                        PicStr = [NSString stringWithFormat:@"%.2f",[[shopM objectForKey:@"ActAmount"] floatValue]];
                        Pic1Str = [NSString stringWithFormat:@"%.2f",[[shopM objectForKey:@"ActDeductionAmount"]floatValue]];
                        break;
                    }
                }

                [cell.contentView addSubview:[self setFrame:CGRectMake(35, 0, screenMySize.size.width-45, 30) setTitle:[NSString stringWithFormat:@"%@",MStr] setFont:labelSize-2 setColor:[UIColor redColor]]];
                
                
                [cell.contentView addSubview:[self setFrame:CGRectMake(35, 30+tabeHight*dic.count, screenMySize.size.width/2-35, 30) setTitle:[NSString stringWithFormat:@"小计:¥%@",PicStr] setFont:labelSize-2 setColor:[UIColor redColor]]];
                
                [cell.contentView addSubview:[self setFrame:CGRectMake(screenMySize.size.width/2, 30+tabeHight*dic.count, screenMySize.size.width/2-10, 30) setTitle:[NSString stringWithFormat:@"返现:¥%@",Pic1Str] setFont:labelSize-2 setColor:[UIColor grayColor]]];
            }
            


            UIImageView *bgImageView = [[UIImageView alloc] initWithFrame:CGRectMake(35,Hight+10+tabeHight*i, tabeHight-20,tabeHight-20)];
            [bgImageView sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@",[[[dic objectAtIndex:i] objectForKey:@"Picture"]objectForKey:@"ImageUrl"]]]];
            bgImageView.contentMode =  UIViewContentModeScaleAspectFit;
            //    bgImageView.contentMode = UIViewContentModeScaleAspectFill;
            [cell.contentView addSubview:bgImageView];
            

            UIImageView *bg1ImageView = [[UIImageView alloc] initWithFrame:CGRectMake(34.5,Hight+9.5+tabeHight*i, tabeHight-19,tabeHight-19)];
            [bg1ImageView setImage:[UIImage imageNamed:@"gwckk"]];
            [cell.contentView addSubview:bg1ImageView];
            
            
            [cell.contentView addSubview:[self setFrame:CGRectMake(20+tabeHight, Hight+10+tabeHight*i, screenMySize.size.width-(30+tabeHight), 40) setTitle:[NSString stringWithFormat:@"%@",[[dic objectAtIndex:i] objectForKey:@"ProductName"]] setFont:labelSize setColor:[UIColor blackColor]]];
            
            [cell.contentView addSubview:[self setFrame:CGRectMake(20+tabeHight, Hight+10+40+tabeHight*i, screenMySize.size.width-(30+tabeHight), 20) setTitle:[NSString stringWithFormat:@"%@",[[dic objectAtIndex:i] objectForKey:@"CustomerDiscountUnitPrice"]] setFont:labelSize setColor:[UIColor blackColor]]];
            
            UIButton *inButton = [UIButton buttonWithType:UIButtonTypeCustom];
            CGRect frame = CGRectMake(34.5,Hight+9.5+tabeHight*i, tabeHight-19,tabeHight-19);
            [inButton setFrame:frame];
            inButton.tag = (indexPath.section+1)*detailTag+indexPath.row*reTag+i;
            [inButton addTarget:self action:@selector(deAction:) forControlEvents:UIControlEventTouchUpInside];
            [cell.contentView addSubview:inButton];
            
            if (![[NSString stringWithFormat:@"%@",[[dic objectAtIndex:i] objectForKey:@"Disabled"]] isEqualToString:@"0"])
            {
                
                UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(35,Hight+10+tabeHight-40+tabeHight*i, tabeHight-20,20)];
                titleLabel.text = @"[已下架]";
                titleLabel.font = [UIFont fontWithName:@"TrebuchetMS-Bold" size:labelSize];
                titleLabel.textColor = [UIColor redColor];
                titleLabel.textAlignment = 1;
                titleLabel.backgroundColor = [UIColor colorWithWhite:0 alpha:.4];
                [cell.contentView addSubview:titleLabel];
            }

 
            [cell.contentView addSubview:[self setButton:CGRectMake(10, (tabeHight-22)/2+Hight+tabeHight*i, 22, 22) setBool:NO setTag:(indexPath.section+2)*gTag+indexPath.row*reTag+i]];
            
  
            UIImage *loginImage = [UIImage imageNamed:@"jj_01"];
            UIImage *loginImage1 = [UIImage imageNamed:@"jj_00"];
            UIButton * reButton = [UIButton buttonWithType:UIButtonTypeCustom];
            [reButton setFrame:CGRectMake(20+tabeHight, Hight+70+tabeHight*i, 51/2, 52/2)];
            [reButton setBackgroundImage:loginImage forState:UIControlStateNormal];
            [reButton setBackgroundImage:loginImage1 forState:UIControlStateDisabled];
            [reButton addTarget:self action:@selector(reAction:) forControlEvents:UIControlEventTouchUpInside];
            reButton.tag = (indexPath.section+1)*reDTag+indexPath.row*reTag+i;
            [cell.contentView addSubview:reButton];
            
            UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"jj_02"]];
            imageView.frame = CGRectMake(20+tabeHight+51/2, Hight+70+tabeHight*i, 80/2, 52/2);
            [cell.contentView addSubview:imageView];
            
            UITextField *textField = [[UITextField alloc] initWithFrame:imageView.frame];
            textField.backgroundColor = [UIColor clearColor];
            [textField setFont:[UIFont systemFontOfSize:labelSize-1]];
            textField.returnKeyType = UIReturnKeyGo;
            textField.keyboardType = UIKeyboardTypeNumberPad;
            textField.delegate = self;
            textField.text = [NSString stringWithFormat:@"%@",[[dic objectAtIndex:i] objectForKey:@"Quantity"]];
            textField.textAlignment = 1;
            textField.tag = (indexPath.section+1)*textTag+indexPath.row*reTag+i;
            [cell.contentView addSubview:textField];
            
            
            UIImage *loginImage2 = [UIImage imageNamed:@"jj_03"];
            UIImage *loginImage3 = [UIImage imageNamed:@"jj_04"];
            UIButton *addButton3 = [UIButton buttonWithType:UIButtonTypeCustom];
            [addButton3 setFrame:CGRectMake(20+tabeHight+51/2+80/2, Hight+70+tabeHight*i, 51/2, 52/2)];
            [addButton3 setBackgroundImage:loginImage2 forState:UIControlStateNormal];
            [addButton3 setBackgroundImage:loginImage3 forState:UIControlStateDisabled];
            [addButton3 addTarget:self action:@selector(addAction:) forControlEvents:UIControlEventTouchUpInside];
            addButton3.tag = (indexPath.section+1)*addTag+indexPath.row*reTag+i;
            [cell.contentView addSubview:addButton3];
            
        }
    }
    return cell;
}

-(UILabel*)setFrame:(CGRect)frame setTitle:(NSString*)title setFont:(int)font setColor:(UIColor*)myColor
{
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:frame];
    if (title != nil)
        titleLabel.text = [NSString stringWithFormat:@"%@",title];
    titleLabel.textAlignment = 0;
    titleLabel.numberOfLines = 0;
    titleLabel.font = [UIFont fontWithName:@"Arial" size:font];
    titleLabel.textColor = myColor;
    return titleLabel;
}




-(UIView*)setButton:(CGRect)frame setBool:(BOOL)yBOOL setTag:(int)Tag
{
    int indexRow = Tag%gTag;
    int indexSection = Tag/gTag-2;
    int indexRoww = (Tag%gTag)/reTag;
    int indexI = Tag%reTag;

    NSMutableArray *dicG = nil;
    yBOOL = YES;
    
    if (indexRow == 0)
    {
        dicG = [[gArray objectAtIndex:indexSection] objectAtIndex:0];
        indexI = 0;
        
        if ([[NSString stringWithFormat:@"%@",[[dicG objectAtIndex:indexI] objectForKey:@"allgKey"]] isEqualToString:@"0"])
        {
            yBOOL = NO;//未选择
        }
    }
    else
    {
        dicG = [[gArray objectAtIndex:indexSection] objectAtIndex:indexRoww-1];
        
        if ([[NSString stringWithFormat:@"%@",[[dicG objectAtIndex:indexI] objectForKey:@"gKey"]] isEqualToString:@"0"])
        {
            yBOOL = NO;//未选择
        }
    }
    
    UIButton *xButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [xButton setFrame:frame];
    if (yBOOL)
        [xButton setBackgroundImage:[UIImage imageNamed:@"xzPress"] forState:UIControlStateNormal];
    else
        [xButton setBackgroundImage:[UIImage imageNamed:@"xzPre"] forState:UIControlStateNormal];
    xButton.tag = Tag;
    [xButton addTarget:self action:@selector(xAction:) forControlEvents:UIControlEventTouchUpInside];
    return xButton;
}

-(void)goSection:(int)indexSection goI:(int)indexI goRow:(int)indexRoww setQuantity:(int)intQ setField:(bool)FBOOL
{
    NSMutableArray *dic = [[shopArray objectAtIndex:indexSection] objectAtIndex:indexRoww-1];
    
    NSString *quantityString =nil;
    if (FBOOL)
        quantityString = [NSString stringWithFormat:@"%d",intQ];
    else
        quantityString = [NSString stringWithFormat:@"%d",[[NSString stringWithFormat:@"%@",[[dic objectAtIndex:indexI] objectForKey:@"Quantity"]] intValue]+intQ];
    if ([quantityString isEqualToString:@"0"]) {
        return;
    }
    NSMutableDictionary *m = [[[shopArray objectAtIndex:indexSection] objectAtIndex:indexRoww-1] objectAtIndex:indexI];
    NSMutableDictionary *FlowSetting = [NSMutableDictionary dictionaryWithDictionary:m];
    [FlowSetting setValue:quantityString forKey:@"Quantity"];
    [[[shopArray objectAtIndex:indexSection] objectAtIndex:indexRoww-1] setObject:FlowSetting atIndexedSubscript:indexI];
    
    [self updata];
//    [mTableView reloadData];
//    [self allMoney];
}

-(void)reAction:(id)sender
{
    int Tag = ((UIButton*)sender).tag;
    int indexSection = Tag/reDTag-1;
    int indexRoww = (Tag%reDTag)/reTag;
    int indexI = Tag%reTag;
    
    [self goSection:indexSection goI:indexI goRow:indexRoww setQuantity:-1 setField:NO];
}

-(void)addAction:(id)sender
{
    int Tag = ((UIButton*)sender).tag;
    int indexSection = Tag/addTag-1;
    int indexRoww = (Tag%addTag)/reTag;
    int indexI = Tag%reTag;
    
    [self goSection:indexSection goI:indexI goRow:indexRoww setQuantity:+1 setField:NO];
}

//限制输入长度 by zwb 2011.9.6
- (BOOL)textField:(UITextField *)text1Field shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    
//    NSString *text = [text1Field.text stringByReplacingCharactersInRange:range withString:string];
//    int type1 = [text intValue];
// 判断未做

    return YES;
    
}

- (BOOL)textFieldShouldEndEditing:(UITextField *)textField
{
    int type1 = [textField.text intValue];
    if (type1 == 0) {
        textField.text = @"1";
        type1 = [textField.text intValue];
    }
    int Tag = textField.tag;
    int indexSection = Tag/textTag-1;
    int indexRow = (Tag%textTag)/reTag;
    int indexI = Tag%reTag;
    
    [self goSection:indexSection goI:indexI goRow:indexRow setQuantity:type1 setField:YES];
    
    return YES;
}

//滚动或者触摸隐藏键盘
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    [[UIApplication sharedApplication] sendAction:@selector(resignFirstResponder) to:nil from:nil forEvent:nil];
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [[UIApplication sharedApplication] sendAction:@selector(resignFirstResponder) to:nil from:nil forEvent:nil];
}

-(void)xAction:(id)sender
{
    int Tag = ((UIButton*)sender).tag;
    
    int indexRow = Tag%gTag;
    int indexSection = Tag/gTag-2;
    int indexRoww = (Tag%gTag)/reTag;
    int indexI = Tag%reTag;
    
    if (indexRow == 0)//总
    {
        NSMutableArray *m = [gArray objectAtIndex:indexSection];
        BOOL NyesorNo = NO;
        for ( int i = 0; i < m.count; i ++)
        {
            
            if (i == 0)
            {
                if ([[NSString stringWithFormat:@"%@",[[[m objectAtIndex:i]objectAtIndex:0] objectForKey:@"allgKey"]] isEqualToString:@"0"])
                {
                    NyesorNo = YES;
                }
                else
                    NyesorNo = NO;
            }
            NSMutableArray *mSetArray = [[NSMutableArray alloc] init];
            for (int j = 0; j < [[m objectAtIndex:i] count]; j++)
            {
                NSMutableDictionary *FlowSetting = [NSMutableDictionary dictionaryWithCapacity:1];
                if (NyesorNo)
                {
                    if (![[NSString stringWithFormat:@"%@",[[[[shopArray objectAtIndex:indexSection] objectAtIndex:i] objectAtIndex:indexI] objectForKey:@"Disabled"]] isEqualToString:@"0"])
                    {
                        [FlowSetting setValue:@"0" forKey:@"gKey"];
                        [FlowSetting setValue:@"1" forKey:@"allgKey"];
                        
                    }
                    else
                    {
                        [FlowSetting setValue:@"1" forKey:@"gKey"];
                        [FlowSetting setValue:@"1" forKey:@"allgKey"];
                    }
                }
                else
                {
                    [FlowSetting setValue:@"0" forKey:@"gKey"];
                    [FlowSetting setValue:@"0" forKey:@"allgKey"];
                }
                [mSetArray addObject:FlowSetting];
            }
            [[gArray objectAtIndex:indexSection] setObject:mSetArray atIndexedSubscript:i];
        }
    }
    else
    {
        NSMutableArray *m = [[gArray objectAtIndex:indexSection] objectAtIndex:indexRoww-1];
        
        NSMutableDictionary *FlowSetting = [NSMutableDictionary dictionaryWithDictionary:[m objectAtIndex:indexI]];
        if ([[NSString stringWithFormat:@"%@",[FlowSetting objectForKey:@"gKey"]] isEqualToString:@"0"]&&[[NSString stringWithFormat:@"%@",[[[[shopArray objectAtIndex:indexSection] objectAtIndex:indexRoww-1] objectAtIndex:indexI] objectForKey:@"Disabled"]] isEqualToString:@"0"])
        {
           [FlowSetting setValue:@"1" forKey:@"gKey"];
        }
        else
            [FlowSetting setValue:@"0" forKey:@"gKey"];
        
        [[[gArray objectAtIndex:indexSection] objectAtIndex:indexRoww-1] setObject:FlowSetting atIndexedSubscript:indexI];
        
        
        NSMutableArray *m1 = [[gArray objectAtIndex:indexSection] objectAtIndex:indexRoww-1];
        BOOL NyesorNo = NO;
        for ( int i = 0; i < m1.count; i ++)
        {
            NSMutableDictionary *FlowSetting = [NSMutableDictionary dictionaryWithDictionary:[m1 objectAtIndex:i]];
            if ([[NSString stringWithFormat:@"%@",[FlowSetting objectForKey:@"gKey"]] isEqualToString:@"0"]&&[[NSString stringWithFormat:@"%@",[[[[shopArray objectAtIndex:indexSection] objectAtIndex:indexRoww-1] objectAtIndex:i] objectForKey:@"Disabled"]] isEqualToString:@"0"])
            {
                NyesorNo = YES;
            }
        }
        NSMutableDictionary *FlowSetting1 = [NSMutableDictionary dictionaryWithDictionary:[[[gArray objectAtIndex:indexSection] objectAtIndex:0] objectAtIndex:0]];
        if (!NyesorNo)
        {
            [FlowSetting1 setValue:@"1" forKey:@"allgKey"];
        }
        else
        {
            [FlowSetting1 setValue:@"0" forKey:@"allgKey"];
        }
        [[[gArray objectAtIndex:indexSection] objectAtIndex:0] setObject:FlowSetting1 atIndexedSubscript:0];
    }
    [self updata];
    
}

- (UITableViewCell *)mCell:(UITableViewCell*)cell bCTableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0)
    {
        [cell.contentView addSubview:[self setFrame:CGRectMake(10, 10, screenMySize.size.width-10, 20) setTitle:@"猜你喜欢" setFont:labelSize setColor:[UIColor blackColor]]];
    }
    else if (indexPath.row == 1)
    {
        UIScrollView *ta1ScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0,0, self.view.bounds.size.width, screenMySize.size.height*3/5)];
        ta1ScrollView.backgroundColor = [UIColor clearColor];
        [ta1ScrollView setPagingEnabled:NO];
        [ta1ScrollView setShowsHorizontalScrollIndicator:NO];
        [cell.contentView addSubview:ta1ScrollView];
        
        [ta1ScrollView setContentSize:CGSizeMake(self.view.bounds.size.width*xhArray.count/3,screenMySize.size.height*3/5)];
        
        int Hight = 10;
        for (int i = 0; i < xhArray.count; i ++)
        {
            NSMutableDictionary *dictionary = [xhArray objectAtIndex:i];

            
            UIImageView *bgImageView = [[UIImageView alloc] initWithFrame:CGRectMake((screenMySize.size.width/3-(tabeHight-20))/2+screenMySize.size.width*i/3,Hight+10, tabeHight-20,tabeHight-20)];
            [bgImageView sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@",[[dictionary objectForKey:@"DefaultPictureModel" ] objectForKey:@"ImageUrl"]]]];
            bgImageView.contentMode =  UIViewContentModeScaleAspectFit;
            //    bgImageView.contentMode = UIViewContentModeScaleAspectFill;
            [ta1ScrollView addSubview:bgImageView];
            
            UIImageView *bg1ImageView = [[UIImageView alloc] initWithFrame:CGRectMake(bgImageView.frame.origin.x-.5,Hight+9.5, tabeHight-19,tabeHight-19)];
            [bg1ImageView setImage:[UIImage imageNamed:@"gwckk"]];
            [ta1ScrollView addSubview:bg1ImageView];
            
            [ta1ScrollView addSubview:[self setFrame:CGRectMake(bgImageView.frame.origin.x, bgImageView.frame.origin.y+bgImageView.frame.size.height, bgImageView.frame.size.width, 40) setTitle:[NSString stringWithFormat:@"%@",[dictionary objectForKey:@"Name"]] setFont:labelSize setColor:[UIColor blackColor]]];
            
            [ta1ScrollView addSubview:[self setFrame:CGRectMake(bgImageView.frame.origin.x,bgImageView.frame.origin.y+bgImageView.frame.size.height+40, bgImageView.frame.size.width, 20) setTitle:[NSString stringWithFormat:@"%@",[[dictionary objectForKey:@"ProductPrice"] objectForKey:@"Price"]] setFont:labelSize setColor:[UIColor blackColor]]];

            
            
            UIButton *inButton = [UIButton buttonWithType:UIButtonTypeCustom];
            CGRect frame = CGRectMake((screenMySize.size.width/3-(tabeHight-20))/2+screenMySize.size.width*i/3,Hight+10, tabeHight-20+60,tabeHight-20);
            [inButton setFrame:frame];
            inButton.tag = detailsTag+i;
            [inButton addTarget:self action:@selector(cInfAction:) forControlEvents:UIControlEventTouchUpInside];
            [ta1ScrollView addSubview:inButton];
            
            
        }
    }
    return cell;
}

#pragma mark 猜你喜欢 商品详情界面跳转
-(void)cInfAction:(id)sender
{
    int type = ((UIButton*)sender).tag - detailsTag;
    NSMutableDictionary *dictionary = [xhArray objectAtIndex:type];
    GoodsDetailsViewController *details = [[GoodsDetailsViewController alloc] init];
    details.productId = [NSString stringWithFormat:@"%@",[[dictionary objectForKey:@"ReviewOverviewModel"] objectForKey:@"ProductId"]];
    details.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:details animated:YES];
}

#pragma mark 购物车 商品详情界面跳转
-(void)deAction:(id)sender
{
    int Tag = ((UIButton*)sender).tag;
    int indexSection = Tag/detailTag-1;
    int indexRoww = (Tag%detailTag)/reTag;
    int indexI = Tag%reTag;
    
    GoodsDetailsViewController *details = [[GoodsDetailsViewController alloc] init];
    details.productId = [NSString stringWithFormat:@"%@",[[[[shopArray objectAtIndex:indexSection]objectAtIndex:indexRoww-1]objectAtIndex:indexI] objectForKey:@"ProductId"]];
    details.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:details animated:YES];
}
#pragma mark 店铺 界面跳转
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath*)indexPath
{
 
    if (indexPath.section == shopByArray.count)
        return;
    if (indexPath.row == 0)
    {
        ShopsViewController *details = [[ShopsViewController alloc] init];
        details.shopsId = [NSString stringWithFormat:@"%@",[[shopByArray objectAtIndex:indexPath.section] objectForKey:@"VendorId"]];
        details.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:details animated:YES];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
